initweek  = 508;
initSOW   = 388804.50;
c         =299792458;
rng(12);

initialTime = 508*604800;

R  = [1e7,3e7,5e7];
w  = 5e-3*[0.99,0.20123,0.032413];
centers = [0,0,0;0,0,0;0,0,0];


filefolder = '../data/sat3satObsSim/';

% distance measurement
fhanldes = [];
for i = 1:3
    filename   =strcat(filefolder,'H82-Sat0',num2str(i),'-01.txt');
    fhanldes(i)= fopen(filename,'w');
end
tmpemitposes = zeros(10000,3);
index  =1;
for deltatime = 1:1:1
    sats      =  [1,2,3];
    index1    = ceil(3*rand());
    emitsat      = index1;
    sats(index1) = [];
    index2    = ceil(2*rand());
    recsat      =  sats(index2);
%     recsat = 1;
%     emitsat=2;
%     disp([sat1,sat2]);
    recsatpos   = satpos(w(recsat),deltatime,R(recsat),centers(recsat,:));
    tmpemitpos  = satpos(w(emitsat),deltatime,R(emitsat),centers(emitsat,:));
    distance    = norm(recsatpos-tmpemitpos);
    tmpemittime = deltatime;
    deltatranstime   = distance/c;
    olddistance = distance;
    olddeltatranstime    = deltatranstime;
    iteration=1;
    while 1
        tmpemittime = tmpemittime - deltatranstime;
        tmpemitpos  = satpos(w(emitsat),tmpemittime,R(emitsat),centers(emitsat,:)); 
        distance    = norm(recsatpos-tmpemitpos);
        deltatranstime = (olddistance - distance)/c;
        disp (olddistance - distance);
        if abs(olddistance -distance)<5e-3 || iteration>10
            
            break;
        else 
            iteration  = iteration +1;
        end
    end
    tmpemitposes(index,:) = recsatpos; 
    index =  index+1;
end
plot(tmpemitposes(:,1)',tmpemitposes(:,2)','+');
for i = 1:3
    fclose(fhanldes(i));
end
